<template>
  <div class="icons">
    <swiper :options="swiperOptions2"
            v-if="List.length">
      <swiper-slide v-for="(page, index) of pages"
                    :key="index">
        <div class="icon"
             v-for="item of page"
             :key="item.id">
          <div class="icon-img">
            <img class="img-size"
                 :src="item.imgUrl"
                 :alt="item.desc" />
          </div>
          <p class="icon-content">{{ item.desc }}</p>
        </div>
      </swiper-slide>
    </swiper>
  </div>
</template>

<script>
export default {
  name: "HomeIcons",
  props: {
    List: Array,
  },
  data () {
    return {
      swiperOptions2: {
        loop: true,
      },
    };
  },
  computed: {
    pages () {
      const pages = [];
      this.List.forEach((element, index) => {
        const page = Math.floor(index / 8);
        if (!pages[page]) {
          pages.push([]);
        }
        pages[page].push(element);
      });
      return pages;
    },
  },
};
</script>

<style lang="stylus" scoped>
@import '~@/assets/style/maxin.stylus';

.icons >>> .swiper-slide {
  height: 0;
  padding-bottom: 50%;
  overflow: hidden;
}

.icons {
  margin-top: 0.1rem;

  .icon {
    position: relative;
    float: left;
    overflow: hidden;
    width: 25%;
    height: 0;
    padding-bottom: 25%;

    .icon-img {
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0.44rem;

      .img-size {
        height: 100%;
        display: block;
        margin: 0 auto;
      }
    }

    .icon-content {
      position: absolute;
      bottom: 0;
      left: 0;
      right: 0;
      height: 0.44rem;
      line-height: 0.44rem;
      text-align: center;
      ellipsis();
    }
  }
}
</style>
// 当我们使用axios传递数据时，由于页面刚刚渲染时axios还没有请求，
// 导致List传递过来的是父组件中初始化的值，
// 也就是空数组，导致swiper出现异常，所以要
// 加上一个v-if来给swipei标签的创建加个条件